package com.ting.test.algorithms.大厂刷题班;

import java.util.HashMap;

/**
 * 要求
 * put get setAll() 时间复杂度O(1)
 */
public class 含有SetAll功能的Hash表 {
    private static HashMap<String, Node> hashMap = new HashMap<String, Node>();
    private static int setAllValue = 0;
    private static long setAllTime = -1;


    public static int get(String key) {
        final Node node = hashMap.get(key);
        if (node.time > setAllTime) {
            return node.val;
        } else {
            return setAllValue;
        }
    }

    public static void put(String key, Integer value) {
        Node node = new Node(value, System.currentTimeMillis());
        hashMap.put(key, node);
    }


    public static void seAll(Integer value) {
        setAllValue = value;
        setAllTime = System.currentTimeMillis();
    }

    public static class Node {
        int val;
        long time;

        public Node(int val, long time) {
            this.val = val;
            this.time = time;
        }
    }
}
